package day07;

import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;

@WebServlet("/download")
public class FileDownloadServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("FileDownloadServlet.service()");

        ServletContext application=getServletContext();

        System.out.println("扣积分");

        /*
            attachment:以附件方式打开
            inline:在线打开
         */
        response.setHeader("content-disposition",new String("attachment;filename=aaa啊.zip".getBytes(StandardCharsets.UTF_8),"ISO8859-1"));


        OutputStream out=response.getOutputStream();

        try(FileInputStream in=new FileInputStream(application.getRealPath("/WEB-INF/file/shop.zip"))){

            byte[] b=new byte[20480];

            int n;

            while((n=in.read(b))!=-1){
                out.write(b,0,n);
            }


        }catch (Exception e){
            e.printStackTrace();
        }




    }
}
